﻿@{
    ViewBag.Title = "Apply Request List";
    Layout = "~/Views/Shared/_RequestListLayout.cshtml";
}

<td data-bind="text: CreateDate"></td>
<td data-bind="text: Department"></td>
<td data-bind="text: Project"></td>
<td data-bind="text: Phone"></td>
<td data-bind="text: Description"></td>
<td data-bind="text: Status"></td>


@section Script
 {
    <script type="text/javascript">
        var options = {
            title: "Category Item List",
            searchCriteria: [
                { displayText: "Requester Name", value: ko.observable() }
            ],
            headers: [
                { displayText: "Request Date", value: "CreateDate", width: "auto", canOrder: "false" },
                { displayText: "Department", value: "Department", width: "auto", canOrder: "false" },
                { displayText: "Project", value: "Project", width: "auto", canOrder: "false" },
                { displayText: "Phone", value: "Phone", width: "auto", canOrder: "false" },
                { displayText: "Filters & Comments", value: "Description", width: "auto", canOrder: "false" },
                { displayText: "Status", value: "Status", width: "auto", canOrder: "false" },
                { displayText: "Operation", value: "", width: "auto", canOrder: "false" }
            ],
            defaultOrderBy: "CreateDate",

            dataQueryUrlAccessor: function (viewModel) {
                return appendQueryString('@Url.Action("GetRequests")', {
                    name: viewModel.searchCriteria()[0].value(),
                    pageIndex: viewModel.pageIndex(),
                    orderBy: viewModel.orderBy(),
                    isAsc: viewModel.isAsc()
                });
            },

            dataAddUrlAccessor: function () { return '@Url.Action("Add")'; },
            dataDeleteAccessor: function (data) { return appendQueryString('@Url.Action("Delete")', { id: data.Id }); },

            replaceData: function (data, viewModel) {
                for (var i = 0; i < viewModel.recordSet().length; i++) {
                    var existing = viewModel.recordSet()[i];
                    if (existing.Id == data.Id) {
                        viewModel.recordSet.replace(existing, data);
                        break;
                    }
                }
            },

            removeData: function (data, viewModel) {
                viewModel.recordSet.remove(function (c) {
                    return c.Id == data.Id;
                });
            }
        };

        var viewModel = new ViewModel(options);
        ko.applyBindings(viewModel);

        function appendQueryString(url, parameters) {
            url += "?"
            for (var key in parameters) {
                if (parameters[key] === null || parameters[key] === undefined || parameters[key] === "") {
                    continue;
                }
                url += key + "=" + encodeURIComponent(parameters[key]) + "&";
            }
            return url;
        }
    </script>
}
